
$(function() {
	Tip.emp('责任人');
	
	$("select[id^='emp_t_s']:visible").change(function() {
		var label = $("label[id^='label_emp_t']:visible");
		var a = $(this).val();
		if (a == 'emp') {
			label.html("责任人：");
		} else if (a == 'oper') {
			label.html("录入员：");
		} 
	});
	
	$("select[id^='category_']:visible").selectChain({
		target: $("select[id^='type_']:visible"),
		key: "code",
		value: "name",
		url: 'finance.php?do=Asset.ajax_category',
		data: { ajax: true }
	}).trigger('change');
	
	Tip.emp('使用人');
});

var Asset = {

	dialog_add_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"添加": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.name = form.name.value;
				body.location = form.location.value;
				
				body.category = form.category.value;
				body.type = form.type.value;
				
				body.date = form.date.value;
				body.content = form.content.value;
				body.emp = form.emp.value;
				body.status = form.status.value;
				
				// body.item = data;
				body.item = $.toJSON(data);

				$(this).dialog("close");

				$.post('finance.php?do=Asset.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},

	popup: function (e, p2, id, status) {
		$.cookie('id_', id);

		$.cookie('status_', status);

		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=Asset.edit&id=' + id;	
		var c2 = 'finance.php?do=Asset.view&id=' + id;
		var c3 = 'finance.php?do=Asset.transfer&id=' + id;

		var a1 = ['编辑信息', 'url', 'Asset.dialog_edit_', '650x420'];
		var a2 = ['查看信息', 'url', 'Common.dialog_view_', '650x400'];
		var a3 = ['资产交接', 'url', 'Asset.dialog_transfer_', '650x400'];
		
		/*
		if (status == '1' || status == '2') {
			var d1 = 'finance.php?do=Asset.scrap&id=' + id;
			var b1 = ['申请报废', 'url', 'Asset.dialog_scrap_', '600x400'];
			
			var s = d1 + ", " + c1 + ", " + c2;

			return this.popup_(e, [b1, a1, a2], s);
		}*/
		
		var s = c1 + ", " + c2 + ", " + c3;

		return this.popup_(e, [a1, a2, a3], s);
	},
	
	popup_: function (e, a, s) {
		var id = $.cookie('id_');
		
		var c2= 'finance.php?do=Asset.upload&id=' + id;
		var a2 = ['上传附件', 'url', 'Common.dialog_upload', '600x420'];
		
		var c3 = 'finance.php?do=Asset.copy&id=' + id;
		var a3 = ['复制信息', 'url', 'Asset.dialog_copy_', '650x410'];
		
		var c4 = 'finance.php?do=Asset.view&id=' + id + '&view=2';
		var a4 = ['登记表', 'url', 'Common.dialog_view_', '650x400'];
		
		var c5 = 'finance.php?do=Asset.view&id=' + id + '&view=3';
		var a5 = ['标签打印', 'url', 'Common.dialog_view_', '420x250'];
		
		var ss = 'finance.php?do=Asset.ss&id=' + id;
		var bb = ['修改状态', 'url', 'Asset.dialog_ss', '400x200'];
		
		s += ", " + c2 + ", " + c3 + ", " + c4 + ", " + c5 + ", " + ss;
		
		a.push(a2);
		a.push(a3);
		a.push(a4);
		a.push(a5);
		a.push(bb);
		
		return popup(e, a, s);
	},
	
	dialog_ss: function (url, title, xy) {
		result = Common.result();
		$.getJSON('finance.php?do=Asset.ajax_b', function(json) {
			if (json.status == 1) {
				$('#dialog_edit').dialog('option', 'buttons', {
					"关闭": function() {
						$(this).dialog("close");
					},

					"修改": function() {
						if (!confirm("确认是否提交？")) {
							return false;
						}

						var form = $("#form_edit")[0];

						var body = {};
						body.id = form.id.value;
						body.field = form.field.value;
						body.data = form.data.value;
						body.status = form.status.value;

						$(this).dialog("close");

						var url = 'finance.php?do=Asset.ssPost';
						var p2 = $.cookie('p2');
						if (p2 != null) {
							url += p2;
						}

						$.post(url, body, function(data) {
							$(result).html(data);
						});
					}
				});

				dialog_edit(url, title, xy);
			} else {
				_error();
			}
		});
	},

	dialog_edit_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"修改": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.name = form.name.value;
				body.location = form.location.value;
				
				body.category = form.category.value;
				body.type = form.type.value;

				body.date = form.date.value;
				body.content = form.content.value;
				body.emp = form.emp.value;
				body.status = form.status.value;
				
				// body.item = data;
				body.item = $.toJSON(data);

				$(this).dialog("close");
				
				var url = 'finance.php?do=Asset.editPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},

	dialog_copy_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"复制": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.name = form.name.value;
				body.location = form.location.value;
				
				body.category = form.category.value;
				body.type = form.type.value;

				body.date = form.date.value;
				body.content = form.content.value;
				body.emp = form.emp.value;
				body.status = form.status.value;
				
				// body.item = data;
				body.item = $.toJSON(data);

				$(this).dialog("close");

				$.post('finance.php?do=Asset.addPost', body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	dialog_transfer_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_edit').dialog('option', 'buttons', {
			"关闭": function() { 
				$(this).dialog("close");
			},
			
			"提交": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_edit")[0];

				var body = {};
				body.id = form.id.value;
				body.emp = form.old_emp.value;
				body.accepter = form.emp.value;
				
				body.date = form.date.value;
				body.content = form.content.value;

				$(this).dialog("close");
				
				var url = 'finance.php?do=Asset.transferPost';
				var p2 = $.cookie('p2');
				if (p2 != null) {
					url += p2;
				}

				$.post(url, body, function(data) {
					$(result).html(data);
				});
			}
		});

		dialog_edit(url, title, xy);
	},
	
	download: function (key) {
		if (!confirm("确认是否下载？若下载，建议先另存，再打开。")) {
			return false;
		}
		
		var url = 'finance.php?do=Asset.download&key=' + key;
		jswin(url, 'save', '800x600');
		
		return true;
	},
	
	file: function (id, key) {
		if (!confirm("确认是否删除？")) {
			return false;
		}
		
		var c6 = 'finance.php?do=Asset.upload&id=' + id;
		
		var url = 'finance.php?do=Asset.file';
		var body = { };
		body.key = key;
		
		$.post(url, body, function(data) {
			// alert(data);
			$.get(c6, function(data) {
				$('#dialog_edit').html(data);
			});
		});
		
		return true;
	},
	
	dialog_scrap_: function (url, title, xy) {
		var result = Common.result();
		$('#dialog_add').dialog('option', 'buttons', {
			"关闭": function() {
				$(this).dialog("close");
			},

			"申请": function() {
				if (!confirm("确认是否提交？")) {
					return false;
				}

				var form = $("#form_add")[0];

				var body = { };
				body.name = form.name.value;
				body.unit = form.unit.value;
				body.amount = form.amount.value;
				
				body.content = form.content.value;
				body.emp = form.emp.value;

				$(this).dialog("close");

				$.post('finance.php?do=Scrap.addPost', body, function(data) {
					// $(result).html(data);
					alert("申请提交成功！");
				});
			}
		});

		dialog_add(url, title, xy);
	},
	
	popup_all: function (e, p2) {
		p2 = query(p2);
		$.cookie('p2', p2);

		var c1 = 'finance.php?do=Asset.delete' + p2;	
		var c2 = 'finance.php?do=Asset.xls&xls=2' + p2;
		var c3 = 'finance.php?do=Asset.xls&xls=3' + p2;
		var c9 = 'finance.php?do=Asset.xls' + p2;
			
		var a1 = ['删除信息', 'url', 'Common.list_delete'];				
		var a2 = ['登记表', 'url', 'jswin', '1024x768'];
		var a3 = ['标签打印', 'url', 'jswin', '1024x768'];
		var a9 = ['导出Excel', 'url', 'jswin', '1024x768'];

		var s = c1 + ", " + c2 + ", " + c3 + ", " + c9;

		return popup(e, [a1, a2, a3, a9], s);
	}
};
